<?php
/**
 * 充值管理
 * User: admin
 * Date: 2018/3/20
 * Time: 11:42
 */

namespace Admin\Controller;


class RechargeController extends AdminController
{

    /**
     * 后台充值列表
     */
    public function rechargeList(){
        if(IS_ROOT){
            $oper_uid=1;
        }else{
            $map['operator'] =UID;
            $oper_uid=UID;
        }
        $map['type'] = 5;
        $username  =   I('username');
        if($username) {
            $maps['id|username']=   array(intval($username),array('eq',$username),'_multi'=>true);
            $uid = M('ucenter_member')->where($maps)->getField('id');
           $map['uid'] = $uid?$uid:0;
        }
        $list = $this->lists('recharge_order', $map, 'id DESC');
        foreach ($list as &$li){
            if($li['money_type'] ==1){
                $li['amount'] = $li['amount']."元宝";
            }else if($li['money_type'] ==2){
                $li['amount'] = $li['amount']."金币";
            }
            $operator = M('member')->where('uid='.$li['operator'])->find();
            $li['operator_name'] =$operator['nickname'];

            $li['nickname'] = M('member')->where('uid='.$li['uid'])->getField('nickname');
            $li['mobile'] = M('ucenter_member')->where('id='.$li['uid'])->getField('username');
        }
        $this->assign('operator',$oper_uid);
        $this->assign('_list',$list);
        $this->display('rechargeList');
    }

    /**
     * 后台充值元宝
     */
    public function recharge(){
        if(IS_POST){
            $uid= I('uid');
            $amount  = I('amount');
            $map['id'] = $uid;
            $user = M('ucenter_member')->where($map)->find();
          /*  if($amount <= 0 ){
                $this->error('金额必须大于0!');
            }*/
            $member = M('member')->where('uid='.$uid)->find();
            if($user &&$member){
                M('member')->where('uid='.$uid)->setInc('coin',$amount);
                $data['amount'] = $amount;
                $data['operator'] =UID;
                $data['uid'] = $uid;
                $data['type'] = 5;
                $data['create_time'] = time();
                $data['status'] =1;
                $data['order_no'] = time();
                $data['balance'] = $member['coin'];
                $res = M('recharge_order')->add($data);
                if($res){
                    $this->success('充值成功！', U('rechargeList'));
                }else{
                    $this->error('充值失败!');
                }
            }else{
                $this->error('用户不存在！');
            }

        }
        $uid= I('uid');
        $this->assign('uid',$uid);
        $this->display('recharge');

    }

    /**
     * 后台充值金币
     */
    public function recharge_money(){
        if(IS_POST){
            $uid= trim(I('uid'));
            $amount  = I('amount');
            $map['id'] = $uid;
            $user = M('ucenter_member')->where($map)->find();
          /*  if($amount <= 0 ){
                $this->error('金额必须大于0!');
            }*/
            $member = M('member')->where('uid='.$uid)->find();
            if($user &&$member){
                M('member')->where('uid='.$uid)->setInc('amount',$amount);
                $data['amount'] = $amount;
                $data['uid'] = $uid;
                $data['type'] = 5;
                $data['create_time'] = time();
                $data['status'] =1;
                $data['money_type'] =2;
                $data['order_no'] = time();
                $data['operator'] =UID;
                $data['balance'] = $member['amount'];
                $res = M('recharge_order')->add($data);
                if($res){
                    //邀请奖励处理
                    $member = M('member')->where('uid='.$uid)->find();
                    if($member['referrer']){
                        $refer = M('member')->where('uid='.$member['referrer'])->find();
                        if($refer){
                            $coin = $amount*10;
                            M('member')->where('uid='.$member['referrer'])->setInc('coin',$coin);
                            //邀请奖励记录
                            $refer_data['uid']=$member['referrer'];
                            $refer_data['order_no']=time();
                            $refer_data['type']   = 5;
                            $refer_data['money']  =$coin;
                            $refer_data['month']  = date('Ym',time());
                            $refer_data['status'] = 1;
                            $refer_data['b_refer'] =$uid;
                            $refer_data['createtime']=time();
                            $re = M('user_trade')->add($refer_data);
                        }
                    }
                    $this->success('充值成功！', U('rechargeList'));
                }else{
                    $this->error('充值失败!');
                }
            }else{
                $this->error('用户不存在！');
            }
        }

        $uid= I('uid');
        $this->assign('uid',$uid);
        $this->display('recharge_money');

    }

    /**
     * 用户充值记录
     */
    public function user_rechargeList(){
        $map['type'] = array('neq',5);
        $map['status']= 1;
        $username       =   I('username');
        if($username) {
            $maps['id|username']=   array(intval($username),array('eq',$username),'_multi'=>true);
            $uid = M('ucenter_member')->where($maps)->getField('id');
            $map['uid'] = $uid?$uid:0;
        }
        $list = $this->lists('recharge_order', $map, 'id DESC');
        foreach ($list as &$li){
            $li['nickname'] = M('member')->where('uid='.$li['uid'])->getField('nickname');
            $li['mobile'] = M('ucenter_member')->where('id='.$li['uid'])->getField('username');
            if($li['type'] ==1){
                $li['type_txt']= "支付宝";
            }else if($li['type'] ==2){
                $li['type_txt']= "微信";
            }else if($li['type'] ==3){
                $li['type_txt']= "qq支付";
            }else if($li['type'] ==4){
                $li['type_txt']= "代理支付";
            }
        }
        $count = M('recharge_order')->where($map)->field('sum(amount) as sum_money')->find();
        $this->assign('sum_money',$count['sum_money']);
        $this->assign('_list',$list);
        $this->display('user_rechargeList');
    }


    /**
     * 代理充值列表
     */
    public function proxy_recharge(){
        $list = $this->lists('proxy_recharge','', 'id DESC');
        $this->assign('_list',$list);
        $this->display('proxy_recharge');
    }

    /**
     * 添加代理充值
     */
    public function add_proxy(){
        if(IS_POST){
            $post = I('');
            $data['name'] = $post['name'];
            $data['proxy_id'] = $post['proxy_id'];
            $data['uptime'] = time();
            $id = M('proxy_recharge')->add($data);
            $id !== false ? $this->success('新增成功', U('proxy_recharge')) : $this->error('未知错误');
        }

        $this->display('add_proxy');
    }

    /**
     * 编辑代理充值
     */
    public function edit_proxy(){
        $id= I('id');

        if(IS_POST){
            $post = I('');
            $id= $post['id'];
            $data['name'] = $post['name'];
            $data['proxy_id'] = $post['proxy_id'];
            $data['uptime'] = time();
            $id = M('proxy_recharge')->where('id='.$id)->save($data);
            $id !== false ? $this->success('编辑成功', U('proxy_recharge')) : $this->error('未知错误');
        }

        $info = M('proxy_recharge')->where('id='.$id)->find();
        $this->assign('info',$info);
        $this->display('edit_proxy');
    }


    public function delete(){
        $id= I('id');
        $result =M('proxy_recharge')->where('id='.$id)->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }



}